package com.im.service.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.im.service.common.ImConstant;
import com.im.service.domain.entity.ImOrg;
import com.im.service.domain.entity.ImUser;
import com.im.service.domain.vo.AddressBookVO;
import com.im.service.mapper.ImOrgMapper;
import com.im.service.mapper.ImUserMapper;
import com.im.service.service.ImOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * im-组织 服务实现类
 * </p>
 *
 * @author 刘志强
 * @since 2024-07-08
 */
@Service
public class ImOrgServiceImpl extends ServiceImpl<ImOrgMapper, ImOrg> implements ImOrgService {

    @Autowired
    private ImOrgMapper orgMapper;
    @Autowired
    private ImUserMapper userMapper;

    @Override
    public List<AddressBookVO> tree() {
        List<ImOrg> orgs = orgMapper.selectList(null);
        List<ImUser> users = userMapper.selectList(null);
        List<AddressBookVO> root = children(orgs, users, 0L);

//        List<AddressBookVO> orgUsers = users.stream()
//                .filter(imUser -> ObjUtil.notEqual(imUser.getId(), ImConstant.SysUserId))
//                .filter(imUser -> ObjUtil.equal(imUser.getOrgId(), 0L))
//                .map(imUser -> {
//                    AddressBookVO user = new AddressBookVO();
//                    user.setId(imUser.getId());
//                    user.setLabel(imUser.getNickname());
//                    user.setUser(imUser);
//                    user.setType(2);
//                    return user;
//                })
//                .collect(Collectors.toList());
//        root.addAll(orgUsers);
        return root;
    }

    public List<AddressBookVO> children(List<ImOrg> orgs, List<ImUser> users, Long parentId) {
        return orgs.stream()
                .filter(imOrg -> ObjUtil.equal(imOrg.getParentId(), parentId))
                .map(imOrg -> {
                    AddressBookVO addressBookVO = new AddressBookVO();
                    addressBookVO.setId(imOrg.getId());
                    addressBookVO.setLabel(imOrg.getOrgName());
                    addressBookVO.setType(1);
                    addressBookVO.setOrg(imOrg);
                    List<AddressBookVO> orgUsers = users.stream()
                            .filter(imUser -> ObjUtil.equal(imUser.getOrgId(), imOrg.getId()))
                            .map(imUser -> {
                                AddressBookVO user = new AddressBookVO();
                                user.setId(imUser.getId());
                                user.setLabel(imUser.getNickname());
                                user.setUser(imUser);
                                user.setType(2);
                                return user;
                            })
                            .collect(Collectors.toList());

                    List<AddressBookVO> children = children(orgs, users, imOrg.getId());
                    children.addAll(orgUsers);
                    addressBookVO.setChildren(children);
                    return addressBookVO;
                })
                .collect(Collectors.toList());
    }
}
